import util from '@/libs/util';
import getDate from '../../util/getDate';
import Cookies from 'js-cookie';

export default {
    name: 'exportable-table',
    data () {
        return {
            columns: [{
                title: '序号',
                key: 'rownum',
                width: 70,
                align: 'center',
                render: (h, params) => {
                    return h('div', {}, ((this.pageIndex - 1) * this.pageSize + params.index + 1));
                }
            }, {
                title: '套餐名称',
                key: 'name'
            }, {
                title: '费用类型',
                key: 'cost',
                render: (h, params) => {
                    return h('div', {}, params.row.chargeType.name);
                }
            }, {
                title: '免费主题数',
                key: 'themesun'
            }, {
                title: '免费奖状数',
                key: 'freediploma'
            },/* {
                title: '主题费用',
                key: 'themecost'
            }, {
                title: '奖状费用',
                key: 'diplomacost'
            }, {
                title: '下载数量',
                key: 'teenagersun'
            }, {
                title: '下载收费标准',
                key: 'rates'
            }, */{
                title: '操作',
                key: 'action',
                width: 200,
                align: 'center',
                render: (h, params) => {
                    return h('div', [
                        h('Button', {
                            props: {
                                type: 'primary',
                                size: 'small'
                            },
                            style: {
                                marginRight: '5px'
                            },
                            on: {
                                click: () => {
                                    this.update(params.row);
                                }
                            }
                        }, '修改'),
                        h('Button', {
                            props: {
                                type: 'primary',
                                size: 'small'
                            },
                            style: {
                                marginRight: '5px'
                            },
                            on: {
                                click: () => {
                                    this.delete(params.row.id);
                                }
                            }
                        }, '删除')
                    ])
                }
            }],
            query: {
                cost: '',
                name: ''
            },
            data: [],
            formData: {},
            pageIndex: 1,
            pageSize: 10,
            total: 0,
            viewModal: false,
            chargeTypeList: []
        }
    },
    mounted () {
        this.loadList();
    },
    methods: {
        loadList () {
            util.ajax.post('/busine/charge/list', util.toFormData({
                page: this.pageIndex,
                size: this.pageSize,
                cost: (this.query.cost ? this.query.cost : ''),
                name: this.query.name
            })).then(res => {
                this.data = res.data.data.pageInfo.list;
                this.total = res.data.data.pageInfo.total;
                this.chargeTypeList = res.data.data.chargeTypeList;
            });
        },
        refresh () {
            this.query = {
                cost: '',
                name: ''
            };
            this.loadList();
        },
        showAdd () {
            this.formData = {};
            this.viewModal = true;
        },
        update (obj) {
            let formData = {};
            for (let key in obj) {
                formData[key] = obj[key];
            }
            this.formData = formData;
            this.viewModal = true;
        },
        save () {
            let formData = {};
            for (let key in this.formData) {
                formData[key] = this.formData[key];
            }
            formData['createtime'] = getDate.getDate(formData['createtime'], 'time');
            formData['updatetime'] = getDate.getDate(formData['updatetime'], 'time');
            util.ajax.post('/busine/charge/save', util.toFormData(formData)).then(res => {
                if (res.data.message === 'SUCCESS') {
                    this.$Message.success('保存成功！');
                    this.loadList();
                    this.viewModal = false;
                }
            });
        },
        delete (id) {
            this.$Modal.confirm({
                title: '提示',
                content: '<p>确定删除？</p>',
                onOk: () => {
                    util.ajax.post('/busine/charge/delete', util.toFormData({
                        id: id
                    })).then(res => {
                        if (res.data.message === 'SUCCESS') {
                            this.$Message.success('删除成功！');
                            this.loadList();
                        }
                    });
                },
                onCancel: () => {
                }
            });
        }
    }
}